package com.blb.controller;

import com.blb.entity.InventoryCheck;
import com.blb.entity.User;
import com.blb.entity.page.BlbPage;
import com.blb.entity.page.DataTablePage;
import com.blb.service.IInventoryCheckService;
import com.blb.utils.ResponseHelp;
import com.blb.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/*
* 作者：钟宇轩
* 库存查询Controller
* */
@Controller
@RequestMapping("/inventorycheck")
public class InventoryCheckController {
    @Resource
    private IInventoryCheckService service;

    /*
    * 作者：钟宇轩
    * 功能：跳转至库存查询的HTML页面
    * */
    @RequiresPermissions("inventorycheck")
    @GetMapping("showall")
    public String findAll(Model model) {
        return ("warehouse/Inventory_check");
    }
    /*
    * 作者：钟宇轩
    * 功能：传入搜索信息和字段长度等信息，对库存进行搜索
    * */
    @ResponseBody
    @PostMapping("search")
    public BlbPage select(String pNumber,String pName,String nodeId,String nodeType,int start, int length){
        User user = ShiroUtils.getUser();
        String companyId = user.getCompanyId();
        List<InventoryCheck> list = service.select(pNumber,pName,start,length,companyId,nodeId,nodeType);
        int count = service.count(pNumber,pName);
        return new DataTablePage(count,count,list);
    }

    /*
    * 作者：钟宇轩
    * 功能：通过公司ID（companyId）查询所有的仓库
    * */
    @ResponseBody
    @GetMapping("allWare")
    public ResponseHelp findAllWare(){
        User user = ShiroUtils.getUser();
         String companyId = user.getCompanyId();
        List<InventoryCheck> allWare = service.findAllWare(companyId);
        ResponseHelp responseHelp = new ResponseHelp();
        responseHelp.setData(allWare);
        return responseHelp;
    }

    /*
    * 作者：钟宇轩
    * 功能：通过前台传回来的批次号（batch）查询该条信息的详细信息
    * */
    @ResponseBody
    @GetMapping("allDetails")
    public ResponseHelp findDetails(int batch){
        InventoryCheck inventoryCheck = service.showDetails(batch);
        ResponseHelp responseHelp = new ResponseHelp();
        responseHelp.setData(inventoryCheck);
        return responseHelp;
    }
}
